define(function (require) {

    var $ = require("zepto");

    var page = require("page");

    var _image = {
        lazy_img: function () {
            var $img = $(".lazy-img");
            var loading_img = "./img/loading_large.gif";
            $img.each(function () {
                var that = $(this);
                if (that.is("img")) {
                    that.attr("src", loading_img);
                } else {
                    var pos = that.css("background-position");
                    var size = that.css("background-size");
                    that.attr({"data-position": pos, "data-size": size});
                    ("no" === that.attr("data-bg")) && that.css({"background-repeat": "no-repeat"});
                    that.css({"background-image": "url('" + loading_img + "')", "background-size": "120px 120px", "background-position": "center"});
                    ("no" === that.attr("data-image")) && that.css({"background-image": "none"});
                }
            });
        },
        lazy_start: function () {
            var that = this;
            setTimeout(function () {
                for (var i = 0; 3 > i; i++) {
                    var _page = $(".m-page").eq(i);
                    if (0 === _page.length) {
                        break;
                    }
                    if (_page.find(".lazy-img").length > 0) {
                        that.lazy_change(_page, !1);
                        "flyCon" === _page.attr("data-page-type") && that.lazy_change($(".m-flypop"), false);
                    }
                }
            }, 200);
        }, lazy_bigP: function () {
            if ($(".lazy-img").length > 0)
                for (var i = 3; 5 >= i; i++) {
                    var _page = $(".m-page").eq(page._pageNow + i);
                    if (0 === _page.length) {
                        break;
                    }
                    if (_page.find(".lazy-img").length > 0) {
                        _image.lazy_change(_page, true);
                        ("flyCon" === _page.attr("data-page-type")) && _image.lazy_change($(".m-flypop"), false);
                    }
                }
        },
        lazy_change: function (t, n) {
            if ("3d" === t.attr("data-page-type") && this.lazy_3d(t), "flyCon" == t.attr("data-page-type")) {
                var $img = $(".m-flypop").find(".lazy-img");
                $img.each(function () {
                    var that = $(this);
                    var src = that.attr("data-src");
                    $("<img />").on("load", function () {
                        that.is("img") && that.attr("src", src);
                    }).attr("src", src);
                });
            }
            var $img = t.find(".lazy-img");
            $img.each(function () {
                var that = $(this);
                var src = that.attr("data-src");
                var pos = that.attr("data-position");
                var size = that.attr("data-size");
                if ("no" !== that.attr("data-bg")) {
                    $("<img />").on("load", function () {
                        if (that.is("img")) {
                            that.attr("src", src);
                        } else {
                            that.css({"background-image": "url(" + src + ")", "background-position": pos, "background-size": size});
                        }
                        if (n)
                            for (var i = 0; $(".m-page").size() > i; i++) {
                                var _page = $(".m-page").eq(i);
                                if ($(".m-page").find(".lazy-img").length > 0) {
                                    _image.lazy_change(_page, true);
                                }
                            }
                    }).attr("src", src), that.removeClass("lazy-img").addClass("lazy-finish");
                } else if ("yes" === that.attr("data-auto")) {
                    that.css("background", "none");
                }
            });
        },
        lazy_load: function () {
            var $img = $(".lazy-img.load");
            $img.each(function () {
                var that = $(this);
                var src = that.attr("data-src");
                var pos = that.attr("data-position");
                var size = that.attr("data-size");
                if ("no" !== that.attr("data-bg")) {
                    $("<img />").on("load", function () {
                        that.is("img") ? that.attr("src", src) : that.css({"background-image": "url(" + src + ")", "background-position": pos, "background-size": size});
                    }).attr("src", src);
                    that.removeClass("lazy-img").addClass("lazy-finish");
                } else if ("yes" === that.attr("data-auto")) {
                    that.css("background", "none");
                }
            });
        }};

    $(function () {
        _image.lazy_img();
    });

    $(window).on("load", function () {
        _image.lazy_start();
        setTimeout(function () {
            _image.lazy_load();
        }, 200);
    });

    return  _image;
});